package com.lcb.manager.web;

import com.alibaba.fastjson.JSON;
import com.lcb.manager.entity.Activity;
import com.lcb.manager.repository.ActivityRepository;
import com.lcb.manager.util.MediaUploadUtil;
import com.lcb.manager.vo.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/activity")
public class ActivityController {

    @Autowired
    private ActivityRepository activityRepository;

    @ResponseBody
    @RequestMapping(value = "/list")
    public String list(@RequestParam int id, @RequestParam(defaultValue = "") String date, @RequestParam int page, @RequestParam int limit) {
        Json result = new Json();
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<Activity> pageResult;
        if (!date.isEmpty()) {
            String year = date.split("-")[0];
            String month = date.split("-")[1];
            pageResult = activityRepository.findByDateOrderByDateAsc(id, year, month, pageable);
        } else {
            pageResult = activityRepository.findByPersonIdOrderByDateAsc(id, pageable);
        }
        result.code = 0;
        result.count = (int) pageResult.getTotalElements();
        result.data = pageResult.getContent();
        return JSON.toJSONStringWithDateFormat(result, "yyyy-MM-dd");
    }

    @RequestMapping(value = "/get_empty_form")
    public String getEmptyForm(ModelMap modelMap) {
        modelMap.put("title", "公益活动");
        modelMap.put("isEdit", false);
        return "_activity_form";
    }

    @RequestMapping(value = "/get_form")
    public String getForm(Model model, @RequestParam int id) {
        Activity activity = activityRepository.findById(id).orElse(null);
        model.addAttribute("title", "公益活动");
        model.addAttribute("isEdit", true);
        if (activity != null) {
            model.addAttribute("vo", activity);
        }
        return "_activity_form";
    }

    @ResponseBody
    @PostMapping("/add")
    public String add(
            @RequestParam("files") MultipartFile[] files,
            @RequestParam("personId") int personId,
            @RequestParam("date") String date,
            @RequestParam("content") String content) throws IOException {
        Json result = new Json();

        Activity activity = new Activity();
        List<String> imageNameList = MediaUploadUtil.uploadImages(files);
        activity.setImgPath(imageNameList.get(0));
        activity.setPersonId(personId);
        activity.setDate(Date.valueOf(date));
        activity.setContent(content);
        activityRepository.save(activity);
        result.code = 0;
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @PostMapping("/edit")
    public String edit(
            @RequestParam("files") MultipartFile[] files,
            @RequestParam("id") int id,
            @RequestParam("date") String date,
            @RequestParam("content") String content) throws IOException {
        Json result = new Json();

        Activity activity = activityRepository.findById(id).orElse(null);
        if (activity != null) {
            List<String> imageNameList = MediaUploadUtil.uploadImages(files);
            activity.setImgPath(imageNameList.get(0));
            activity.setDate(Date.valueOf(date));
            activity.setContent(content);
            activityRepository.save(activity);
        }
        result.code = 0;
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public String delete(@RequestParam int id) {
        Json result = new Json();
        activityRepository.deleteById(id);
        result.code = 0;
        result.msg = "删除成功！";
        return JSON.toJSONString(result);
    }
}
